package com.oocl.operator;

import java.util.Scanner;

import com.oocl.model.Customer;
import com.oocl.mylink.Node;
import com.oocl.rcm.Entry;

public class ImplOperat implements Operat{

	public void select() {
		Entry.list.show();
	}

	public void select(String imformation) {
		String[] info = imformation.split(",");
		Node[] nodes = null;
		for (int i = 0; i < info.length; i++) {
			String[] str = info[i].split(":");
			if(str[0].equals("id")){
				nodes = Entry.list.getById(Entry.list.getNodes(), str[1]);
				break;
			}
			if(i==0){
				if(str[0].equals("name")){
					nodes = Entry.list.getByName(Entry.list.getNodes(), str[1]);
				}
				if(str[0].equals("sex")){
					nodes = Entry.list.getBySex(Entry.list.getNodes(), str[1]);
				}
				if(str[0].equals("address")){
					nodes = Entry.list.getByAddress(Entry.list.getNodes(), str[1]);
				}
				if(str[0].equals("call")){
					nodes = Entry.list.getByCall(Entry.list.getNodes(), str[1]);
				}
				if(str[0].equals("birthday")){
					nodes = Entry.list.getByBirth(Entry.list.getNodes(), str[1]);
				}
			}else{
				if(str[0].equals("id")){
					nodes = Entry.list.getById(nodes, str[1]);
				}
				if(str[0].equals("name")){
					nodes = Entry.list.getByName(nodes, str[1]);
				}
				if(str[0].equals("sex")){
					nodes = Entry.list.getBySex(nodes, str[1]);
				}
				if(str[0].equals("address")){
					nodes = Entry.list.getByAddress(nodes, str[1]);
				}
				if(str[0].equals("call")){
					nodes = Entry.list.getByCall(nodes, str[1]);
				}
				if(str[0].equals("birthday")){
					nodes = Entry.list.getByBirth(nodes, str[1]);
				}else{
					Entry.showError();
					break;
				}
			}
		}
		for (int i = 0; i < nodes.length; i++) {
			if(nodes[i]!=null){
				System.out.println(nodes[i].getData()+"\t");
			}
		}
	}

	public boolean add(String information) {
		Customer customer = Convert.convert(information);
		Entry.list.add(customer);
		System.out.println("Add sucessful!");
		return true;
	}

	public void delete(String id) {
		if(Entry.list.remove(id)){
			Entry.index--;
			System.out.println("delete success!");
		}else{
			System.out.println("not found id!");
		}
	}

	public void update(String imformation) {
		Customer customer = Convert.convert(imformation);
		String[] info = imformation.split(",");
		String[] str = info[0].split(":");
		customer.setId(str[1]);
		if(Entry.list.update(customer)){
			System.out.println("update success!");
		}else{
			System.out.println("update fail!");
		}
	
	}

	public boolean order() {
		Customer[] cus = Entry.list.toCusArray();
		for(int i=0; i<cus.length;i++){
			for(int j=0;j<cus.length-i-1;j++){
				if((cus[j].comparTo(cus[j+1]))>0){
					Customer temp = cus[j];
					cus[j] = cus[j+1];
					cus[j+1] = temp;
				}
				
			}
		}
		for (int i = 0; i < cus.length; i++) {
			System.out.println(cus[i]);
		}
		return false;
	}

	public void help() {
		Entry.showGuide();
	}

	public void quit() {
		System.out.println("Exiting ... ... ...");
		System.out.println("Operat Success,Bye!!!");
		System.exit(0);
	}


	public void receiveInput() {
		Scanner scan = new Scanner(System.in);
		String input = scan.nextLine();
		valid(input);
	}
	
	public void valid(String input){
		String[] str = input.trim().split("\\s+");
		
		if(str.length == 1){
			switch(str[0]){
			case "L":
				select();
				break;
			case "O":
				order();
				break;
			case "H":
				Entry.showGuide();
				break;
			case "Q":
				quit();
				break;
			case "A":
				System.out.println("Customer information could not NULL!");
				break;
			default:
				Entry.showError();
			}
		}else if(str.length == 2){
			switch(str[0]){
			case "A":
				add(str[1]);
				break;
			case "L":
				select(str[1]);
				break;
			case "D":
				delete(str[1]);
				break;
			case "U":
				update(str[1]);
				break;
			case "H":
				if(str[1].equals("A")){
					System.out.println("--------------------------------------");
					System.out.println("Usage:	[Control]  [args...]:");
					System.out.println("H  A" + '\t' + "add a member information acorrding args;");
				}
			}
		}else{
			Entry.showError();
		}
//		
	}

}
